package com.wanchuang.hepos.ui.page.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.wanchuang.architecture.utils.GsonUtils;
import com.wanchuang.architecture.utils.ValidateUtils;
import com.wanchuang.hepos.R;
import com.wanchuang.hepos.bridge.request.HomeRequestViewModel;
import com.wanchuang.hepos.bridge.state.home.KaBaoViewModel;
import com.wanchuang.hepos.databinding.FragmentKaBaoBinding;
import com.wanchuang.hepos.databinding.FragmentZhanDanBinding;
import com.wanchuang.hepos.proto.Bank;
import com.wanchuang.hepos.proto.MemberBank;
import com.wanchuang.hepos.ui.base.BaseFragment;
import com.wanchuang.hepos.ui.dialog.RecycleViewDialog;

import kale.ui.view.dialog.EasyDialog;

/**
 * 文件名 : HomeRootFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/24 16:20
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class KaBaoFragment extends BaseFragment {


    private FragmentKaBaoBinding mBinding;
    private KaBaoViewModel mViewModel;
    private HomeRequestViewModel mRequestViewModel;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(KaBaoViewModel.class);
        mRequestViewModel = ViewModelProviders.of(this).get(HomeRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ka_bao, container, false);
        mBinding = FragmentKaBaoBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mViewModel.layoutManager.set(new LinearLayoutManager(mActivity));

        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRequestViewModel.getErrorString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });
        mRequestViewModel.getResultString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });
        mRequestViewModel.getBankList().observe(this, bankList -> {
            hideProgress();
            mViewModel.adapter.get().replaceData(bankList);
            int size = bankList.size();
            if (size < 10) {
                mViewModel.adapter.get().getLoadMoreModule().loadMoreEnd();
            } else {
                mViewModel.adapter.get().getLoadMoreModule().loadMoreComplete();
            }
        });
        mRequestViewModel.getBanks().observe(this, banks -> {
            mViewModel.banks.addAll(banks);
        });
        mRequestViewModel.get_xin_yong_bank("2", "2");  // 获取信用卡列表
        mRequestViewModel.get_support("2", "1");  // 类型:1-结算卡; 2-信用卡   //VIP支持：1-支持;2-所有
        int type = getArguments().getInt("type");
        if (type == 0) {
            mViewModel.canSeeTwo.set(true);
            mViewModel.renzhenText.set("提额认证");
            mViewModel.pageType.set("提额认证");
        } else {
            mViewModel.canSeeFirst.set(true);
            mViewModel.renzhenText.set("VIP认证");
            mViewModel.pageType.set("VIP认证");
        }
    }

    @Override
    public void loadInitData() {
        super.loadInitData();
    }

    public class ClickProxy {
        public void back() {
            nav().navigateUp();
        }

        public void itemOnClick(int position) {
            MemberBank.bank bank = mViewModel.data.get(position);
            Bundle bundle = new Bundle();
            bundle.putString("data", GsonUtils.serializedToJson(bank));
            nav().navigate(R.id.action_kaBaoFragment_to_xinCardDetailFragment, bundle);
        }

        public void loadMoreListener() {

        }

        public void bindCard() {
            nav().navigate(R.id.action_kaBaoFragment_to_bindXinCardFragment);
        }

        public void showBank() {
            if (ValidateUtils.isValidate(mViewModel.banks)) {
                RecycleViewDialog.Builder builder = new RecycleViewDialog.Builder(mActivity);
                builder.setData(GsonUtils.serializedToJson(mViewModel.banks)).setTitle("VIP收款支持银行");
                EasyDialog build = builder.build();
                build.show(getChildFragmentManager());
            } else {
                showShortToast("暂无支持银行");
            }
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden) {
            showProgress();
            mRequestViewModel.get_xin_yong_bank("2", "2");  // 获取信用
        }
    }
}
